﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using YJKArchUtils.Utils;

namespace YJKRevitAxisModule
{
	public partial class CurveToGridForm : System.Windows.Forms.Form
	{
		public bool ResultSelect
		{
			get
			{
				return this.m_ResultSelect;
			}
		}

		public GridType GetGridType()
		{
			if (this.m_GridTypeList.Count == 0)
			{
				return null;
			}
			return this.m_GridTypeList[this.cmbGridType.SelectedIndex];
		}

		public double ExtendLength()
		{
			return AssistFunc.mmToFeet((double)Convert.ToInt32(this.tbGridExtend.Text));
		}

		public List<string> LayerStyleName()
		{
			List<string> list = new List<string>();
			for (int i = 0; i < this.checkedListBoxLayer.Items.Count; i++)
			{
				if (this.checkedListBoxLayer.GetItemChecked(i))
				{
					list.Add(this.m_LayerStyleNameList[i]);
				}
			}
			return list;
		}

		public void SetLayerStyle(string layerStyleName)
		{
			int num = this.m_LayerStyleNameList.FindIndex((string s) => s.CompareTo(layerStyleName) == 0);
			if (num != -1)
			{
				this.checkedListBoxLayer.SetItemChecked(num, true);
				this.m_NewSelectedLayerIndex = num;
				this.SaveCompornentData();
			}
		}

		public CurveToGridForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
			this.InitializeGraphicsStyleList();
			this.InitializeGridStyle();
		}

		private void InitializeGraphicsStyleList()
		{
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(GraphicsStyle)))
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle != null)
				{
					Category graphicsStyleCategory = graphicsStyle.GraphicsStyleCategory;
					if (graphicsStyleCategory != null)
					{
						Category parent = graphicsStyleCategory.Parent;
						if (parent != null && (parent.Name.CompareTo("线") == 0 || parent.Name.CompareTo("Lines") == 0) && graphicsStyleCategory.Name.CompareTo(graphicsStyle.Name) == 0 && this.checkedListBoxLayer.Items.IndexOf(graphicsStyleCategory.Name) == -1)
						{
							if (graphicsStyleCategory.Id.IntegerValue > 0)
							{
								this.checkedListBoxLayer.Items.Add(graphicsStyleCategory.Name);
								this.m_LayerStyleNameList.Add(graphicsStyleCategory.Name);
							}
							else
							{
								int integerValue = graphicsStyleCategory.Id.IntegerValue;
								if (integerValue == -2000078 || integerValue == -2000288 || integerValue == -2000285 || integerValue == -2000284 || integerValue == -2000287 || integerValue == -2000286 || integerValue == -2000043 || integerValue == -2000044 || integerValue == -2000042 || integerValue == -2009504)
								{
									this.checkedListBoxLayer.Items.Add(graphicsStyleCategory.Name);
									this.m_LayerStyleNameList.Add(graphicsStyleCategory.Name);
								}
							}
						}
					}
				}
			}
		}

		private void InitializeGridStyle()
		{
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(GridType)))
			{
				GridType gridType = element as GridType;
				if (gridType != null)
				{
					this.cmbGridType.Items.Add(gridType.Name);
					this.m_GridTypeList.Add(gridType);
				}
			}
			if (this.cmbGridType.Items.Count > 0)
			{
				this.cmbGridType.SelectedIndex = 0;
			}
		}

		private void SelectLineStyleForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
			if (this.m_NewSelectedLayerIndex < this.checkedListBoxLayer.Items.Count && this.checkedListBoxLayer.GetItemChecked(this.m_NewSelectedLayerIndex))
			{
				this.checkedListBoxLayer.SelectedIndex = this.m_NewSelectedLayerIndex;
			}
		}

		private void SelectLineStyleForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			if (this.LayerStyleName().Count == 0 && !this.m_ResultSelect)
			{
				YJKMessageBox.Show("基线图层名不能为空");
				e.Cancel = true;
				return;
			}
			this.SaveCompornentData();
		}

		private void btnSelectLayer_Click(object sender, EventArgs e)
		{
			this.m_ResultSelect = true;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.m_ResultSelect = false;
		}

		private void tbGridExtend_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int num = int.Parse(this.tbGridExtend.Text);
				if (Geometry.LessThan((double)num, 0.0) || Geometry.GreaterThan((double)num, 1000000000.0))
				{
					YJKMessageBox.Show("输入值无效.");
					e.Cancel = true;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Show("输入值无效.");
				e.Cancel = true;
			}
		}

		private void LoadCompornentData()
		{
			string path = Path.GetTempPath() + "CurveToGridForm.dat";
			if (!File.Exists(path))
			{
				this.tbGridExtend.Text = "1000";
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num >= this.m_GridTypeList.Count)
			{
				num = 0;
			}
			this.cmbGridType.SelectedIndex = num;
			this.tbGridExtend.Text = streamReader.ReadLine();
			try
			{
				this.m_NewSelectedLayerIndex = Convert.ToInt32(streamReader.ReadLine());
			}
			catch (Exception)
			{
				this.m_NewSelectedLayerIndex = 0;
				streamReader.Close();
				fileStream.Close();
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "CurveToGridForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.cmbGridType.SelectedIndex);
			streamWriter.WriteLine(this.tbGridExtend.Text);
			streamWriter.WriteLine(this.m_NewSelectedLayerIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void checkedListBoxLayer_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.checkedListBoxLayer.SelectedItem != null && ((ListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.checkedListBoxLayer.ClearSelected();
			}
		}

		private void checkedListBoxLayer_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.checkedListBoxLayer.SelectedItem != null && ((ListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.checkedListBoxLayer.ClearSelected();
			}
		}

		private ExternalCommandData m_Revit;

		private List<string> m_LayerStyleNameList = new List<string>();

		private List<GridType> m_GridTypeList = new List<GridType>();

		private bool m_ResultSelect;

		private int m_NewSelectedLayerIndex;
	}
}
